package com.labManage.util;

import com.labManage.vo.BorrowHandleState;
import com.labManage.vo.GoodsButtonWord;
import com.labManage.vo.GoodsHandleState;

public class GoodsStateUtil {
    private static GoodsStates[] goodsWordTable = new GoodsStates[]{
            new GoodsStates("在库", GoodsHandleState.ALLOW_BORROW, true, GoodsButtonWord.BORROW, true),
            new GoodsStates("在库", GoodsHandleState.BORROW_APPLIED, true, GoodsButtonWord.BORROW_APPLYING, true),
            new GoodsStates("在库", GoodsHandleState.BAN_BORROW, true, GoodsButtonWord.BAN_BORROW, true),

            new GoodsStates("在库", GoodsHandleState.ALLOW_BORROW, true, GoodsButtonWord.BORROW, false),
            new GoodsStates("在库", GoodsHandleState.BORROW_APPLIED, true, GoodsButtonWord.BORROW, false),
            new GoodsStates("在库", GoodsHandleState.BAN_BORROW, true, GoodsButtonWord.BAN_BORROW, false),

            new GoodsStates("不在库", GoodsHandleState.ALLOW_REBACK, true, GoodsButtonWord.REBACK, true),
            new GoodsStates("不在库", GoodsHandleState.ALLOW_REBACK, false, GoodsButtonWord.BORROWING, true),
            new GoodsStates("不在库", GoodsHandleState.REBACK_APPLIED, true, GoodsButtonWord.REBACK_APPLYING, true)
    };

    public static String getGoodsHandleStateChanged(Boolean needReback, String nowGoodsHandleState, Boolean selfHandle) {
        if (nowGoodsHandleState.equals(GoodsHandleState.ALLOW_BORROW)) {
            return GoodsHandleState.BORROW_APPLIED;
        } else if ((needReback == false) && (nowGoodsHandleState.equals(GoodsHandleState.BORROW_APPLIED))) {
            return GoodsHandleState.ALLOW_BORROW;
        } else if ((nowGoodsHandleState.equals(GoodsHandleState.ALLOW_REBACK)) && (selfHandle == true)) {
            return GoodsHandleState.REBACK_APPLIED;
        } else {
            return null;
        }
    }

    public static String getBorrowHandleStateChanged(Boolean needReback, String nowGoodsHandleState, Boolean selfHandle) {
        if ((nowGoodsHandleState.equals(GoodsHandleState.ALLOW_BORROW) && (needReback == true))) {
            return BorrowHandleState.BORROW_APPLYING;
        } else if ((nowGoodsHandleState.equals(GoodsHandleState.ALLOW_BORROW) && (needReback == false))) {
            return BorrowHandleState.BORROW_APPLYING;
        } else if ((nowGoodsHandleState.equals(GoodsHandleState.BORROW_APPLIED) && (needReback == false))) {
            return BorrowHandleState.BORROW_APPLYING;
        } else if ((nowGoodsHandleState.equals(GoodsHandleState.ALLOW_REBACK)) && (selfHandle == true)) {
            return BorrowHandleState.REBACK_APPLYING;
        } else {
            return null;
        }
    }

    public static Boolean getGoodsButtonEnable(Boolean needReback, String nowGoodsHandleState,Boolean selfHandle) {
        if ((needReback == true) & (nowGoodsHandleState.equals(GoodsHandleState.ALLOW_BORROW))) {
            return true;
        } else if ((needReback == true) & (nowGoodsHandleState.equals(GoodsHandleState.BORROW_APPLIED))) {
            return false;
        } else if ((needReback == true) & (nowGoodsHandleState.equals(GoodsHandleState.BAN_BORROW))) {
            return false;
        } else if ((needReback == false) & (nowGoodsHandleState.equals(GoodsHandleState.ALLOW_BORROW))) {
            return true;
        } else if ((needReback == false) & (nowGoodsHandleState.equals(GoodsHandleState.BORROW_APPLIED))) {
            return true;
        } else if ((needReback == false) & (nowGoodsHandleState.equals(GoodsHandleState.BAN_BORROW))) {
            return false;
        } else if ((needReback == true) & (nowGoodsHandleState.equals(GoodsHandleState.ALLOW_REBACK))) {
            if(selfHandle==true){
                return true;
            }else{
                return false;
            }

        } else if ((needReback == true) & (nowGoodsHandleState.equals(GoodsHandleState.REBACK_APPLIED))) {
            return false;
        } else {
            return false;
        }
    }

    public static String getGoodsButtonWord(String goodsState, Boolean selfHandle, String goodsHandleState, Boolean needReback) {
        String word = "";
        String tableGoodsState = "";
        String tableGoodsHandleState = "";
        Boolean tableSelfHandle = false;
        Boolean tableNeedReback = false;
        for (int i = 0; i < goodsWordTable.length; i++) {
            tableGoodsState = goodsWordTable[i].goodsState.trim();
            tableGoodsHandleState = goodsWordTable[i].goodsHandleState.trim();
            tableSelfHandle = goodsWordTable[i].selfHandle;
            tableNeedReback = goodsWordTable[i].needReback;

            boolean b1 = tableGoodsState.equals(goodsState.trim());
            boolean b2 = tableGoodsHandleState.equals(goodsHandleState.trim());
            boolean b3 = tableNeedReback == needReback;
            if (b1 && b2 && b3) {
                if ((tableGoodsState.equals("不在库")) && (tableGoodsHandleState.equals(GoodsHandleState.ALLOW_REBACK))) {
                    if (tableSelfHandle == selfHandle) {
                        return goodsWordTable[i].goodsWord;
                    }
                } else {
                    return goodsWordTable[i].goodsWord;
                }
            }
        }
        return null;
    }
}

class GoodsStates {
    String goodsState;
    String goodsHandleState;
    Boolean selfHandle;
    String goodsWord;
    Boolean needReback;

    public GoodsStates(String goodsState, String goodsHandleState, Boolean selfHandle, String goodsWord, Boolean needReback) {
        this.goodsState = goodsState;
        this.goodsHandleState = goodsHandleState;
        this.selfHandle = selfHandle;
        this.goodsWord = goodsWord;
        this.needReback = needReback;
    }
}

